import { appDefaultConfig } from '@/config/app_config'
import { getItem, setItem } from '@/utils/utils'
import { HOME_ROUTE_NAME } from '@/router/constants'
const tagsName = 'tagsViewList'
const state = {
  sidebarOpened: true, // 控制侧边栏是否展开
  // 系统相关配置
  appConfig: {
    ...appDefaultConfig
  },
  tagsViewList: getItem(tagsName) || []
}

const mutations = {
  triggerSidebarOpened(state) {
    state.sidebarOpened = !state.sidebarOpened
  },
  setAppConfig(state, values) {
    state.appConfig = {
      ...values
    }
  },
  addTagsViewList(state, value) {
    // 如果在 tagsView 里面存在就不添加
    let isFind = state.tagsViewList.find((item) => item.path === value.path)
    if (value.fullPath === `/${HOME_ROUTE_NAME}`) {
      // 如果是 首页也不添加
      isFind = true
    }
    if (!isFind) {
      state.tagsViewList.push(value)
      setItem(tagsName, state.tagsViewList)
    }
  },
  removeTagsView(state, route) {
    state.tagsViewList = state.tagsViewList.filter(
      (item) => item.fullPath !== route.fullPath
    )
    setItem(tagsName, state.tagsViewList)
  }
}
const actions = {}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
